home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Python / thread_solaris.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  4.6 KB  |  219 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include </usr/include/thread.h>
  6. #undef _POSIX_THREADS
  7.  
  8.  
  9. /*
  10.  * Initialization.
  11.  */
  12. static void PyThread__init_thread _P0()
  13. {
  14. }
  15.  
  16. /*
  17.  * Thread support.
  18.  */
  19. struct func_arg {
  20.     void (*func) _P((void *));
  21.     void *arg;
  22. };
  23.  
  24. static void *new_func _P1(funcarg, void *funcarg)
  25. {
  26.     void (*func) _P((void *));
  27.     void *arg;
  28.  
  29.     func = ((struct func_arg *) funcarg)->func;
  30.     arg = ((struct func_arg *) funcarg)->arg;
  31.     free(funcarg);
  32.     (*func)(arg);
  33.     return 0;
  34. }
  35.  
  36.  
  37. int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
  38. {
  39.     struct func_arg *funcarg;
  40.     int success = 0;    /* init not needed when SOLARIS_THREADS and */
  41.                 /* C_THREADS implemented properly */
  42.  
  43.     dprintf(("PyThread_start_new_thread called\n"));
  44.     if (!initialized)
  45.         PyThread_init_thread();
  46.     funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
  47.     funcarg->func = func;
  48.     funcarg->arg = arg;
  49.     if (thr_create(0, 0, new_func, funcarg,
  50.                THR_DETACHED | THR_NEW_LWP, 0)) {
  51.         perror("thr_create");
  52.         free((void *) funcarg);
  53.         success = -1;
  54.     }
  55.     return success < 0 ? 0 : 1;
  56. }
  57.  
  58. long PyThread_get_thread_ident _P0()
  59. {
  60.     if (!initialized)
  61.         PyThread_init_thread();
  62.     return thr_self();
  63. }
  64.  
  65. static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup)
  66. {
  67.     dprintf(("PyThread_exit_thread called\n"));
  68.     if (!initialized)
  69.         if (no_cleanup)
  70.             _exit(0);
  71.         else
  72.             exit(0);
  73.     thr_exit(0);
  74. }
  75.  
  76. void PyThread_exit_thread _P0()
  77. {
  78.     do_PyThread_exit_thread(0);
  79. }
  80.  
  81. void PyThread__exit_thread _P0()
  82. {
  83.     do_PyThread_exit_thread(1);
  84. }
  85.  
  86. #ifndef NO_EXIT_PROG
  87. static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
  88. {
  89.     dprintf(("PyThread_exit_prog(%d) called\n", status));
  90.     if (!initialized)
  91.         if (no_cleanup)
  92.             _exit(status);
  93.         else
  94.             exit(status);
  95.     if (no_cleanup)
  96.         _exit(status);
  97.     else
  98.         exit(status);
  99. }
  100.  
  101. void PyThread_exit_prog _P1(status, int status)
  102. {
  103.     do_PyThread_exit_prog(status, 0);
  104. }
  105.  
  106. void PyThread__exit_prog _P1(status, int status)
  107. {
  108.     do_PyThread_exit_prog(status, 1);
  109. }
  110. #endif /* NO_EXIT_PROG */
  111.  
  112. /*
  113.  * Lock support.
  114.  */
  115. PyThread_type_lock PyThread_allocate_lock _P0()
  116. {
  117.     mutex_t *lock;
  118.  
  119.     dprintf(("PyThread_allocate_lock called\n"));
  120.     if (!initialized)
  121.         PyThread_init_thread();
  122.  
  123.     lock = (mutex_t *) malloc(sizeof(mutex_t));
  124.     if (mutex_init(lock, USYNC_THREAD, 0)) {
  125.         perror("mutex_init");
  126.         free((void *) lock);
  127.         lock = 0;
  128.     }
  129.     dprintf(("PyThread_allocate_lock() -> %lx\n", (long)lock));
  130.     return (PyThread_type_lock) lock;
  131. }
  132.  
  133. void PyThread_free_lock _P1(lock, PyThread_type_lock lock)
  134. {
  135.     dprintf(("PyThread_free_lock(%lx) called\n", (long)lock));
  136.     mutex_destroy((mutex_t *) lock);
  137.     free((void *) lock);
  138. }
  139.  
  140. int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag)
  141. {
  142.     int success;
  143.  
  144.     dprintf(("PyThread_acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
  145.     if (waitflag)
  146.         success = mutex_lock((mutex_t *) lock);
  147.     else
  148.         success = mutex_trylock((mutex_t *) lock);
  149.     if (success < 0)
  150.         perror(waitflag ? "mutex_lock" : "mutex_trylock");
  151.     else
  152.         success = !success; /* solaris does it the other way round */
  153.     dprintf(("PyThread_acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
  154.     return success;
  155. }
  156.  
  157. void PyThread_release_lock _P1(lock, PyThread_type_lock lock)
  158. {
  159.     dprintf(("PyThread_release_lock(%lx) called\n", (long)lock));
  160.     if (mutex_unlock((mutex_t *) lock))
  161.         perror("mutex_unlock");
  162. }
  163.  
  164. /*
  165.  * Semaphore support.
  166.  */
  167. PyThread_type_sema PyThread_allocate_sema _P1(value, int value)
  168. {
  169.     sema_t *sema;
  170.     dprintf(("PyThread_allocate_sema called\n"));
  171.     if (!initialized)
  172.         PyThread_init_thread();
  173.  
  174.     sema = (sema_t *) malloc(sizeof(sema_t));
  175.     if (sema_init(sema, value, USYNC_THREAD, 0)) {
  176.         perror("sema_init");
  177.         free((void *) sema);
  178.         sema = 0;
  179.     }
  180.     dprintf(("PyThread_allocate_sema() -> %lx\n", (long) sema));
  181.     return (PyThread_type_sema) sema;
  182. }
  183.  
  184. void PyThread_free_sema _P1(sema, PyThread_type_sema sema)
  185. {
  186.     dprintf(("PyThread_free_sema(%lx) called\n", (long) sema));
  187.     if (sema_destroy((sema_t *) sema))
  188.         perror("sema_destroy");
  189.     free((void *) sema);
  190. }
  191.  
  192. int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag)
  193. {
  194.     int success;
  195.  
  196.     dprintf(("PyThread_down_sema(%lx) called\n", (long) sema));
  197.     if (waitflag)
  198.         success = sema_wait((sema_t *) sema);
  199.     else
  200.         success = sema_trywait((sema_t *) sema);
  201.     if (success < 0) {
  202.         if (errno == EBUSY)
  203.             success = 0;
  204.         else
  205.             perror("sema_wait");
  206.     }
  207.     else
  208.         success = !success;
  209.     dprintf(("PyThread_down_sema(%lx) return %d\n", (long) sema, success));
  210.     return success;
  211. }
  212.  
  213. void PyThread_up_sema _P1(sema, PyThread_type_sema sema)
  214. {
  215.     dprintf(("PyThread_up_sema(%lx)\n", (long) sema));
  216.     if (sema_post((sema_t *) sema))
  217.         perror("sema_post");
  218. }
  219.